package cn.nbt.busManage.controller;

import cn.nbt.busManage.common.ResponseResult;
import cn.nbt.busManage.model.dto.DriverDto;
import cn.nbt.busManage.model.dto.ScheduleDto;
import cn.nbt.busManage.model.vo.DriverVo;
import cn.nbt.busManage.model.vo.ScheduleVo;
import cn.nbt.busManage.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lyq
 * @time 2024/6/19 19:05
 */

@RestController
@RequestMapping("/schedule")
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    @GetMapping("/list")
    public ResponseResult<Object> list(){
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            List<ScheduleDto> all = scheduleService.findAll();
            ArrayList<ScheduleVo> scheduleVos = new ArrayList<>();
            for(ScheduleDto scheduleDto : all){
                scheduleVos.add(new ScheduleVo(scheduleDto));
            }
            result.Success("查询成功",scheduleVos);
        }catch (Exception e){
            result.Error("查询失败",e.getMessage());
        }
        return result;
    }

    /**
     * 根据信息查询司机
     * @param query 查询信息（名称）
     * @return 司机信息
     */
    @GetMapping("/findByName")
    public ResponseResult<Object> findByName(@RequestParam(name = "query") String query ) {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            List<ScheduleDto> scheduleDtoList = scheduleService.findByName(query);
            if(scheduleDtoList.isEmpty()){
                result.BadRequest();
                return result;
            }
            ArrayList<ScheduleVo> scheduleVos = new ArrayList<>();
            for (ScheduleDto scheduleDto : scheduleDtoList) {
                scheduleVos.add(new ScheduleVo(scheduleDto));
            }
            result.Success("查询成功",scheduleVos);
        }catch (Exception e){
            result.Error(e.getMessage());
        }
        return result;
    }

    /**
     * 根据信息查询司机
     * @param driverId 查询信息（id）
     * @return 司机信息
     */
    @GetMapping("/findById")
    public ResponseResult<Object> findById(@RequestParam(name = "id") Integer driverId) {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            ScheduleDto scheduleDto= (ScheduleDto) scheduleService.findByDriverId(driverId);
            if (scheduleDto==null){
                result.BadRequest();
                return result;
            }
            ScheduleVo scheduleVo = new ScheduleVo(scheduleDto);
            result.Success("查询成功",scheduleVo);
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error();
        }
        return result;
    }


    @GetMapping("/findByScheduleId")
    public ResponseResult<Object> findByScheduleId(@RequestParam(name = "id") Integer id) {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            ScheduleDto scheduleDto = scheduleService.findByScheduleId(id);
            if (scheduleDto==null){
                result.BadRequest();
                return result;
            }
            ScheduleVo scheduleVo = new ScheduleVo(scheduleDto);
            result.Success("查询成功",scheduleVo);
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error();
        }
        return result;
    }

}
